package com.zhj.bz.datastructure.sparsearray;

import java.io.*;

/**
 * @author zhj
 */
public class SparseArray {

    private static final String CHESS_FILE = "data/chess.txt";

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        int chessArr1[][] = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        System.out.println("稀疏数组");
        System.out.println("---------------------------");
        System.out.println("原始棋盘：");
        for (int[] row : chessArr1) {
            for (int i : row) {
                System.out.print(i + "\t");
            }
            System.out.println();
        }
        int[][] sparseArray = toSparseArray(chessArr1);
        System.out.println("压缩后的棋盘（稀疏数组）：");
        for (int[] row : sparseArray) {
            for (int i : row) {
                System.out.print(i + "\t");
            }
            System.out.println();
        }
        System.out.println("存档~");
        write(sparseArray);
        System.out.println("继续上一局~");
        System.out.println("读档~");
        sparseArray = read();
        int[][] arr = toArr(sparseArray);
        System.out.println("解压后的棋盘：");
        for (int[] row : arr) {
            for (int i : row) {
                System.out.print(i + "\t");
            }
            System.out.println();
        }
    }

    /**
     * 解压
     * @param sparseArray 稀疏数组
     * @return
     */
    private static int[][] toArr(int[][] sparseArray) {
        int[][] arr = new int[sparseArray[0][0]][sparseArray[0][1]];
        for (int i = 0; i < sparseArray[0][2]; i++) {
            arr[sparseArray[i+1][0]][sparseArray[i+1][1]] = sparseArray[i+1][2];
        }
        return arr;
    }

    /**
     * 压缩
     * @param arr 原始数组
     * @return
     */
    public static int[][] toSparseArray(int[][] arr) {
        int sum = 0;
        for (int[] row : arr) {
            for (int i : row) {
                if (i != 0) {
                    sum++;
                }
            }
        }
        int [][] sparseArr = new int[sum+1][3];
        sparseArr[0][0] = arr.length;
        sparseArr[0][1] = arr[0].length;
        sparseArr[0][2] = sum;
        sum = 1;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (arr[i][j] != 0) {
                    sparseArr[sum][0] = i;
                    sparseArr[sum][1] = j;
                    sparseArr[sum][2] = arr[i][j];
                    sum++;
                }
            }
        }
        return sparseArr;
    }

    /**
     * 存档
     * @param sparseArray
     * @throws IOException
     */
    public static void write(int[][] sparseArray) throws IOException {
        File file = new File(CHESS_FILE);
        OutputStream os = null;
        ObjectOutputStream oos = null;
        try {
            os = new FileOutputStream(file);
            oos = new ObjectOutputStream(os);
            oos.writeObject(sparseArray);
        } finally {
            if (null != oos)
                oos.close();
            if (null != os)
                os.close();
        }
    }

    /**
     * 读档
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static int[][] read() throws IOException, ClassNotFoundException {
        File file = new File(CHESS_FILE);
        InputStream is = null;
        ObjectInputStream ois = null;
        try {
            is = new FileInputStream(file);
            ois = new ObjectInputStream(is);
            return (int[][]) ois.readObject();
        } finally {
            if (null != ois)
                ois.close();
            if (null != is)
                is.close();
        }
    }
}
